package com.rayboot.footpoint.view;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.rayboot.footpoint.R;
import com.rayboot.footpoint.model.Footprint;
import com.rayboot.footpoint.utils.Util;

public class PlayButton extends ImageView implements View.OnClickListener,
		MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
	public static final int IDLE_STATE = 0;
	public static final int PLAYING_STATE = 1;
	public static final int PAUSE_STATE = 2;
	public static final int NO_ERROR = 0;
	public static final int SDCARD_ACCESS_ERROR = 1;
	public static final int INTERNAL_ERROR = 2;
	private static final String TAG = "PlayButton";
	private Footprint mFootprint;
	OnStateChangedListener mOnStateChangedListener = null;
	private MediaPlayer mPlayer = null;
	File mSampleFile = null;
	public int mSampleLength = 0;
	long mSampleStart = 0L;
	int mState = 0;

	public PlayButton(Context paramContext) {
		super(paramContext);
		initView();
		initFunction();
	}

	public PlayButton(Context paramContext, int paramInt1, int paramInt2) {
		super(paramContext);
		initView();
		initFunction();
	}

	public PlayButton(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet, 0);
		initView();
		initFunction();
	}

	public PlayButton(Context paramContext, AttributeSet paramAttributeSet,
			int paramInt) {
		super(paramContext, paramAttributeSet, paramInt);
		initView();
		initFunction();
	}

	private void controlKeepScreenOn(int state) {
		switch (state) {
		case IDLE_STATE:
		case PAUSE_STATE:
			((Activity) getContext()).getWindow().clearFlags(WindowManager.LayoutParams.ALPHA_CHANGED);
			break;
		case PLAYING_STATE:
			((Activity) getContext()).getWindow().addFlags(WindowManager.LayoutParams.ALPHA_CHANGED);
			break;
		}
	}

	private void initFunction() {
	}

	private void initView() {
		setBackgroundResource(R.drawable.common_footprint_button);
		setScaleType(ImageView.ScaleType.CENTER);
		setIcon(this.mState);
		setOnClickListener(this);
	}

	private void setError(int paramInt) {
		if (this.mOnStateChangedListener != null)
			this.mOnStateChangedListener.onError(paramInt);
	}

	private void setIcon(int state) {
		switch (state) {
		case IDLE_STATE:
			setImageResource(R.drawable.icon_footprint_play);
			break;
		case PLAYING_STATE:
			setImageResource(R.drawable.icon_footprint_pause);
			break;
		case PAUSE_STATE:
			setImageResource(R.drawable.icon_footprint_play);
			break;
		}
	}

	private void setState(int state) {
		if (state == this.mState) {
			return;
		}

		mState = state;
		setIcon(this.mState);
		signalStateChanged(this.mState);
		controlKeepScreenOn(this.mState);
	}

	private void signalStateChanged(int paramInt) {
		if (this.mOnStateChangedListener != null)
			this.mOnStateChangedListener.onStateChanged(paramInt);
	}

	private void startPlayback() {
		stopPlayback();
		this.mPlayer = new MediaPlayer();
		if (mSampleFile == null) {
			return;
		}

		try {
			this.mPlayer.setDataSource(this.mSampleFile.getAbsolutePath());
			this.mPlayer.setOnCompletionListener(this);
			this.mPlayer.setOnErrorListener(this);
			this.mPlayer.prepare();
			this.mPlayer.start();
			this.mSampleStart = System.currentTimeMillis();
			setState(PLAYING_STATE);
		} catch (IllegalArgumentException localIllegalArgumentException) {
			setError(INTERNAL_ERROR);
			this.mPlayer = null;
		} catch (IOException localIOException) {
			setError(SDCARD_ACCESS_ERROR);
			this.mPlayer = null;
		}
	}

	private void togglePlayback() {
		if (mPlayer == null) {
			return;
		}

		if (mState != IDLE_STATE) {
			if (!mPlayer.isPlaying()) {
				mPlayer.start();
				setState(PLAYING_STATE);
			} else {
				mPlayer.pause();
				setState(PAUSE_STATE);
			}
		}
	}

	@Override
	public void onClick(View paramView) {
		if ((this.mSampleFile == null) || (!this.mSampleFile.exists())) {
			Util.showToast(getContext(), R.string.no_voice_data);
			return;
		}

		if (mState == IDLE_STATE) {
			startPlayback();
		} else if (mState == PAUSE_STATE) {
			togglePlayback();
		} else if (mState == PLAYING_STATE) {
			togglePlayback();
		}
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		stopPlayback();
		setImageResource(R.drawable.icon_footprint_play);
	}

	@Override
	public boolean onError(MediaPlayer mp, int errorCode, int paramInt2) {
		setError(errorCode);
		setImageResource(R.drawable.icon_footprint_play);
		return true;
	}

	public void setOnStateChangedListener(
			OnStateChangedListener paramOnStateChangedListener) {
		this.mOnStateChangedListener = paramOnStateChangedListener;
	}

	public void setfootprintData(Footprint footprint) {
		if (footprint != null) {
			this.mFootprint = footprint;
			if (Util.checkAudioData(this.mFootprint)) {
				this.mSampleFile = new File(this.mFootprint.mAudioPath);
				if (!this.mSampleFile.exists())
					this.mSampleFile = null;
			}
		}
	}

	public void stopPlayback() {
		if (mPlayer == null) {
			return;
		}

		this.mPlayer.stop();
		this.mPlayer.release();
		this.mPlayer = null;
		setState(IDLE_STATE);
	}

	public static abstract interface OnCheckedChangeListener {
		public abstract void onCheckedChanged(PlayButton paramPlayButton,
				boolean paramBoolean);
	}

	public static abstract interface OnStateChangedListener {
		public abstract void onError(int paramInt);

		public abstract void onStateChanged(int paramInt);
	}
}
